from flask import render_template, request
from . import main
from .models import Couple

@main.route('/couples')
def couples_list():
    # 基本参数
    page = request.args.get('page', 1, type=int)
    season = request.args.get('season', type=int)
    
    # 获取所有夫妻数据
    query = Couple.query
    
    # 如果指定了季数，添加过滤条件
    if season:
        query = query.filter_by(season=season)
    
    # 获取所有季数（用于下拉菜单）
    all_seasons = sorted(set(c.season for c in Couple.query.all()))
    
    # 获取分页后的夫妻列表
    couples = query.order_by(Couple.season.desc()).paginate(
        page=page, per_page=12, error_out=False
    )
    
    # 渲染模板
    return render_template(
        'couples_list.html',
        couples=couples,
        seasons=all_seasons,
        current_season=season
    )